// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-11-13 17:44:14
// 生成路径: internal/app/system/logic/runners.go
// 生成人：gfast
// desc:跑手管理
// company:云南奇讯科技有限公司
// ==========================================================================

package runners

import (
	"context"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/internal/app/system/dao"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterRunners(New())
}

func New() service.IRunners {
	return &sRunners{}
}

type sRunners struct{}

func (s *sRunners) List(ctx context.Context, req *model.RunnersSearchReq) (listRes *model.RunnersSearchRes, err error) {
	listRes = new(model.RunnersSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.Runners.Ctx(ctx).WithAll()
		if req.Userid != "" {
			m = m.Where(dao.Runners.Columns().Userid+" = ?", gconv.Int(req.Userid))
		}
		if req.SchoolId != "" {
			m = m.Where(dao.Runners.Columns().SchoolId+" = ?", gconv.Int(req.SchoolId))
		}
		if req.Score != "" {
			m = m.Where(dao.Runners.Columns().Score+" = ?", gconv.Int(req.Score))
		}
		if req.StCardFrontImg != "" {
			m = m.Where(dao.Runners.Columns().StCardFrontImg+" = ?", req.StCardFrontImg)
		}
		if req.StCardBackImg != "" {
			m = m.Where(dao.Runners.Columns().StCardBackImg+" = ?", req.StCardBackImg)
		}
		if req.IsConfirm != "" {
			m = m.Where(dao.Runners.Columns().IsConfirm+" = ?", gconv.Int(req.IsConfirm))
		}
		if req.IsBan != "" {
			m = m.Where(dao.Runners.Columns().IsBan+" = ?", gconv.Int(req.IsBan))
		}
		if req.IsDel != "" {
			m = m.Where(dao.Runners.Columns().IsDel+" = ?", gconv.Int(req.IsDel))
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.Runners.Columns().CreatedAt+" >=? AND "+dao.Runners.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.RunnersListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.RunnersListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.RunnersListRes{
				Id:             v.Id,
				SchoolId:       v.SchoolId,
				Money:          v.Money,
				Score:          v.Score,
				StCardFrontImg: v.StCardFrontImg,
				StCardBackImg:  v.StCardBackImg,
				IsConfirm:      v.IsConfirm,
				IsBan:          v.IsBan,
				IsDel:          v.IsDel,
				CreatedAt:      v.CreatedAt,
			}
		}
	})
	return
}

func (s *sRunners) GetById(ctx context.Context, id int) (res *model.RunnersInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.Runners.Ctx(ctx).WithAll().Where(dao.Runners.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sRunners) Add(ctx context.Context, req *model.RunnersAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Runners.Ctx(ctx).Insert(do.Runners{
			IsConfirm: req.IsConfirm,
			IsBan:     req.IsBan,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sRunners) Edit(ctx context.Context, req *model.RunnersEditReq) (err error) {

	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		err = g.Try(ctx, func(ctx context.Context) {
			_, err = dao.Runners.Ctx(ctx).WherePri(req.Id).Update(do.Runners{
				IsConfirm: req.IsConfirm,
				IsBan:     req.IsBan,
			})
			liberr.ErrIsNil(ctx, err, "修改失败")

			_, err = dao.RunnerVerifyRecords.Ctx(ctx).Insert(do.RunnerVerifyRecords{
				RunnerId:  req.Id,
				IsConfirm: req.IsConfirm,
				AdminId:   service.Context().GetUserId(ctx),
				Reason:    req.Reason,
			})
			liberr.ErrIsNil(ctx, err, "修改")
		})
		return err
	})
	return
}

func (s *sRunners) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Runners.Ctx(ctx).Delete(dao.Runners.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
